import requests
import json

BASE_URL = 'http://127.0.0.1:8080/api'
session = requests.Session()
token = None

def test_login():
    print("\n测试登录接口...")
    response = session.post(
        f'{BASE_URL}/employee/login/',
        json={'username': 'admin', 'password': '123456'}
    )
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")
    if response.status_code == 200:
        data = response.json()
        if data.get('code') == 1:
            global token
            token = data['data']['token']
            session.headers.update({'Authorization': f'Bearer {token}'})
            return True
    return False

def test_employees(token):
    print("\n测试员工管理接口...")
    headers = {'Authorization': f'Bearer {token}'}
    response = session.get(f'{BASE_URL}/employee/', headers=headers)
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")

def test_products(token):
    print("\n测试产品管理接口...")
    headers = {'Authorization': f'Bearer {token}'}
    response = session.get(f'{BASE_URL}/product/', headers=headers)
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")

def test_suppliers(token):
    print("\n测试供应商管理接口...")
    headers = {'Authorization': f'Bearer {token}'}
    response = session.get(f'{BASE_URL}/supplier/', headers=headers)
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")

def test_customers(token):
    print("\n测试客户管理接口...")
    headers = {'Authorization': f'Bearer {token}'}
    response = session.get(f'{BASE_URL}/customer/', headers=headers)
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")

def test_stock(token):
    print("\n测试库存管理接口...")
    headers = {'Authorization': f'Bearer {token}'}
    response = session.get(f'{BASE_URL}/stock-record/', headers=headers)
    print(f"状态码: {response.status_code}")
    print(f"响应内容: {response.text}")

def test_stock_in():
    print("\n测试入库单接口...")
    
    # 创建入库单
    data = {
        "supplier_id": 1,
        "details": [
            {
                "product_id": 1,
                "price": 100.00,
                "quantity": 10
            }
        ]
    }
    response = session.post(f'{BASE_URL}/stock-in/', json=data)
    print(f"创建入库单 - 状态码: {response.status_code}")
    print(f"响应内容: {response.text}")
    
    # 获取入库单列表
    response = session.get(f'{BASE_URL}/stock-in/')
    print(f"获取入库单列表 - 状态码: {response.status_code}")
    
    # 获取入库单统计
    response = session.get(f'{BASE_URL}/stock-in/statistics/')
    print(f"获取入库单统计 - 状态码: {response.status_code}")

def test_stock_out():
    print("\n测试出库单接口...")
    
    # 创建出库单
    data = {
        "customer_id": 1,
        "details": [
            {
                "product_id": 1,
                "price": 120.00,
                "quantity": 5
            }
        ]
    }
    response = session.post(f'{BASE_URL}/stock-out/', json=data)
    print(f"创建出库单 - 状态码: {response.status_code}")
    print(f"响应内容: {response.text}")
    
    # 获取出库单列表
    response = session.get(f'{BASE_URL}/stock-out/')
    print(f"获取出库单列表 - 状态码: {response.status_code}")
    
    # 获取出库单统计
    response = session.get(f'{BASE_URL}/stock-out/statistics/')
    print(f"获取出库单统计 - 状态码: {response.status_code}")

def test_stock_check():
    print("\n测试盘点单接口...")
    
    # 创建盘点单
    data = {
        "details": [
            {
                "product_id": 1,
                "actual_stock": 95
            }
        ]
    }
    response = session.post(f'{BASE_URL}/stock-check/', json=data)
    print(f"创建盘点单 - 状态码: {response.status_code}")
    print(f"响应内容: {response.text}")
    
    # 获取盘点单列表
    response = session.get(f'{BASE_URL}/stock-check/')
    print(f"获取盘点单列表 - 状态码: {response.status_code}")
    
    # 获取盘点单统计
    response = session.get(f'{BASE_URL}/stock-check/statistics/')
    print(f"获取盘点单统计 - 状态码: {response.status_code}")

def test_stock_transfer():
    print("\n测试调拨单接口...")
    
    # 创建调拨单
    data = {
        "from_warehouse": 1,
        "to_warehouse": 2,
        "details": [
            {
                "product_id": 1,
                "quantity": 5
            }
        ]
    }
    response = session.post(f'{BASE_URL}/stock-transfer/', json=data)
    print(f"创建调拨单 - 状态码: {response.status_code}")
    print(f"响应内容: {response.text}")
    
    # 获取调拨单列表
    response = session.get(f'{BASE_URL}/stock-transfer/')
    print(f"获取调拨单列表 - 状态码: {response.status_code}")
    
    # 获取调拨单统计
    response = session.get(f'{BASE_URL}/stock-transfer/statistics/')
    print(f"获取调拨单统计 - 状态码: {response.status_code}")

def test_stock_record():
    print("\n测试库存记录接口...")
    
    # 获取库存记录列表
    response = session.get(f'{BASE_URL}/stock-record/')
    print(f"获取库存记录列表 - 状态码: {response.status_code}")
    
    # 导出库存记录
    response = session.get(f'{BASE_URL}/stock-record/export/')
    print(f"导出库存记录 - 状态码: {response.status_code}")

def test_stock_warning():
    print("\n测试库存预警接口...")
    
    # 获取库存预警列表
    response = session.get(f'{BASE_URL}/stock-warning/')
    print(f"获取库存预警列表 - 状态码: {response.status_code}")
    
    # 获取库存预警统计
    response = session.get(f'{BASE_URL}/stock-out/stock_warning/')
    print(f"获取库存预警统计 - 状态码: {response.status_code}")

def main():
    if not test_login():
        print("登录失败，无法继续测试")
        return
    
    test_employees(token)
    test_products(token)
    test_suppliers(token)
    test_customers(token)
    test_stock(token)
    test_stock_in()
    test_stock_out()
    test_stock_check()
    test_stock_transfer()
    test_stock_record()
    test_stock_warning()

if __name__ == '__main__':
    main() 